package org.leo.study.logtrace.config;

import lombok.extern.slf4j.Slf4j;
import org.leo.study.logtrace.common.interceptor.LogTraceInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>
 * web MVC 配置
 * </p>
 *
 * @Package: org.leo.study.config
 * @ClassName: WebMvcConfigurer
 * @author: Li.Junlin
 * @date: 2019.10.10 10:29
 */
@Slf4j
@Configuration
public class WebMvcConfigurerImpl implements WebMvcConfigurer {


    /**
     * 把我们的拦截器注入为bean
     *
     * @return
     */
    @Bean
    public HandlerInterceptor LogTraceInterceptor() {
        log.debug("创建日志追踪拦截器...");
        return new LogTraceInterceptor();
    }

    /**
     * 注册拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.debug("注册日志追踪拦截器...");
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(LogTraceInterceptor());
        // addPathPatterns 用于添加拦截规则, 这里假设拦截 /url 后面的全部链接
        interceptorRegistration.addPathPatterns("/**");
        // excludePathPatterns 用户排除拦截
//        interceptorRegistration.excludePathPatterns("/**");
    }
}
